package com.chinacoal.hr.levelimpl.utils;

import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

/**
 * @author: YunTao.Li
 * @create: 2019-11-17 12:32
 * @description: dao切面，用与添加创建时间，创建人等信息
 **/
@Aspect
@Component
public class AspectDao {

    /**
     * insert切面，捕捉com.chinacoal.hr.levelimpl.dao包下的所有类的insert*方法
     *
     * @param point :
     * @return : java.lang.Object
     * @author : YunTao.Li
     * @date : 2019/11/17 2019/11/17
     */
    @Around("execution(* com.chinacoal.hr.levelimpl.dao.*.insert*(..))")
    public Object insertPointcut(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        if (args != null && args.length > 0) {
            Object obj = args[0];
            try {
                String createBy = (String) obj.getClass().getDeclaredMethod("getCreatedBy").invoke(obj);
                if (StringUtils.isBlank(createBy)) {
                    obj.getClass().getDeclaredMethod("setCreatedBy", String.class).invoke(obj, "testcreateby");
                }
            } catch (NoSuchMethodException e) {
            }
            try {
                String createByName = (String) obj.getClass().getDeclaredMethod("getCreatedByName").invoke(obj);
                if (StringUtils.isBlank(createByName)) {
                    obj.getClass().getDeclaredMethod("setCreatedByName", String.class).invoke(obj, "testcreatebyname");
                }
            } catch (NoSuchMethodException e) {
            }
            try {
                obj.getClass().getDeclaredMethod("setCreationDate", Date.class).invoke(obj, Calendar.getInstance().getTime());
            } catch (NoSuchMethodException e) {
            }

            try {
                obj.getClass().getDeclaredMethod("setLastUpdatedBy", String.class).invoke(obj, "lastUpdateBy");
            } catch (NoSuchMethodException e) {
            }
            try {
                obj.getClass().getDeclaredMethod("setLastUpdateDate", Date.class).invoke(obj, Calendar.getInstance().getTime());
            } catch (NoSuchMethodException e) {
            }
        }


        Object returnValue = point.proceed(args);
        return returnValue;
    }

    /**
     * update切面，捕捉com.chinacoal.hr.levelimpl.dao包下的所有类的update*方法
     *
     * @param point :
     * @return : java.lang.Object
     * @author : YunTao.Li
     * @date : 2019/11/26 2019/11/26
     */
    @Around("execution(* com.chinacoal.hr.levelimpl.dao.*.update*(..))")
    public Object updatePointcut(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        if (args != null && args.length > 0) {
            Object obj = args[0];
            try {
                obj.getClass().getDeclaredMethod("setLastUpdatedBy", String.class).invoke(obj, "lastUpdateBy");
            } catch (NoSuchMethodException e) {
                System.out.println("insert setLastUpdatedBy exception");
            }
            try {
                obj.getClass().getDeclaredMethod("setLastUpdateDate", Date.class).invoke(obj, Calendar.getInstance().getTime());
            } catch (NoSuchMethodException e) {
                System.out.println("insert setLastUpdateDate exception");
            }
        }

        Object returnValue = point.proceed(args);
        return returnValue;
    }

    /**
     * 对象填充方法
     *
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/12/6 2019/12/6
     */
    public void objectFillHandleFor() {

    }
}
